<template></template>

<script>
import Stomp from "stompjs";

export default {
  data() {
    return {
      client: null,
      back_data: "",
      MQTT_SERVICE: "ws://192.168.66.32:15674/ws", // mqtt服务地址
      MQTT_USERNAME: "admin", // 连接用户名
      MQTT_PASSWORD: "admin", // 连接
      RMQ_EXCHANGE: "/exchange/three_model", // 交换机
      RMQ_KEY: "/data", // 交换机routing key
    };
  },
  created() {
    //初始化连接
    this.connect();
  },
  methods: {
    //webSocket连接成功后回调函数
    onConnected(frame) {
      console.log("Connected: " + frame);
      //绑定交换机exchange_pushmsg是交换机的名字rk_pushmsg是绑定的路由key
      const exchange = this.RMQ_EXCHANGE + this.RMQ_KEY;
      // var exchange = "/exchange/exchange_pushmsg/rk_pushmsg";
      //创建随机队列用上面的路由key绑定交换机,放入收到消息后的回调函数和失败的回调函数
      this.client.subscribe(exchange, this.responseCallback, this.onFailed);
      // console.log(frame);
    },
    // 失败后报错
    onFailed(frame) {
      console.log("Failed: " + frame);
      // this.connect();
      // 失败后  等待10秒后重新连接
      setTimeout(() => {
        this.connect();
      }, 1000 * 10);
    },
    // 监听事件
    responseCallback(frame) {
      // console.log("msg=> ", frame.body);
      console.log("msg=> ", JSON.parse(frame.body));
      // this.back_data = JSON.parse(frame.body);
      // console.log(frame);

      // {"MessageId":"787c86505159436d92f78b1b5c403fb0","QueueName":"model-data","ClientId":"DZROBOT_RobA","OrderCode":"DZ-2324","LineNo":"1","DeviceType":"3","DeviceCode":"01","Message":"[112.109619140625,6.1505184173584,-8.14612865447998,24.0342407226563,48.4037246704102,0]","Timestamp":"2024-06-03 19:32:46.3482"}
      // {"MessageId":"d8690c0082dc48f68f99032e28288cd2","QueueName":"model-data","ClientId":"DZROBOT_RobA","OrderCode":"DZ-2324","LineNo":"1","DeviceType":"3","DeviceCode":"01","Message":"[101.416954040527,10.7134952545166,-7.59535598754883,28.5954055786133,48.6171417236328,0]","Timestamp":"2024-06-03 19:32:46.5223"}
      // {"MessageId":"0a6bbc2e4bb1450cb4ccec07fe45e6d4","QueueName":"model-data","ClientId":"DZROBOT_RobA","OrderCode":"DZ-2324","LineNo":"1","DeviceType":"3","DeviceCode":"01","Message":"[89.4804153442383,14.8605241775513,-5.88825511932373,34.0575981140137,46.1806488037109,0]","Timestamp":"2024-06-03 19:32:46.6483"}
      // {"MessageId":"e933ced15ea948dab79f662129ece69d","QueueName":"model-data","ClientId":"DZROBOT_RobA","OrderCode":"DZ-2324","LineNo":"1","DeviceType":"3","DeviceCode":"01","Message":"[79.1622619628906,16.6029777526855,-2.28717613220215,39.5000190734863,38.8696899414063,0]","Timestamp":"2024-06-03 19:32:46.8333"}

      //接收到服务器推送消息，向服务器定义的接收消息routekey路由rk_recivemsg发送确认消息
      // 确认事件会造成不断的来回发送数据
      // this.client.send(
      //   "/exchange/three_model/data",
      //   { "content-type": "text/plain" },
      //   frame.body
      // );
      // this.client.send(
      //   "/exchange/exchange_pushmsg/rk_pushmsg",
      //   { "content-type": "text/plain" },
      //   frame.body
      // );
    },
    connect() {
      //这里填你rabbitMQ的连接ip地址直接替换localhost:15674就好其它的不用改
      this.client = Stomp.client(this.MQTT_SERVICE);
      //填写你rabbitMQ登录的用户名和密码
      const headers = {
        login: this.MQTT_USERNAME,
        passcode: this.MQTT_PASSWORD,
        //虚拟主机，默认“/”
        host: "/",
      };
      //创建连接,放入连接成功和失败回调函数
      this.client.connect(headers, this.onConnected, this.onFailed);
    },
  },
};
</script>

<style>
</style>